package com.ihomey.linkuphome.base;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.akexorcist.localizationactivity.LocalizationActivity;
import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.util.AnnotateUtil;
import com.umeng.analytics.MobclickAgent;


/**
 * 作者：Administrator on 2016/6/24 13:59
 * 邮箱：763522252@qq.com
 */
public class BaseActivity extends LocalizationActivity implements View.OnClickListener {

    private LinearLayout rootLayout;
    private Toolbar toolBar;
    private TextView toolbarTitle;
    private TextView rightTv;
    private MaterialDialog dialog;

    protected void initData() {

    }

    public void showProgressDialog(String content) {
        dialog = new MaterialDialog.Builder(this).backgroundColorRes(R.color.white)
                .content(content).canceledOnTouchOutside(false).contentColorRes(R.color.text_colorPrimaryDark)
                .progress(true, 0)
                .progressIndeterminateStyle(false).build();
        dialog.show();
    }

    public void showProgressDialog(int contentRes) {
        dialog = new MaterialDialog.Builder(this).backgroundColorRes(R.color.white)
                .content(contentRes).canceledOnTouchOutside(false).contentColorRes(R.color.text_colorPrimaryDark)
                .progress(true, 0)
                .progressIndeterminateStyle(false).build();
        dialog.show();
    }

    public void hideProgressDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void showConnectDialog() {
        new MaterialDialog.Builder(this).backgroundColorRes(R.color.white).title("提示").content("当前并无设备处于连接状态，无法进行该操作。请确保蓝牙已经开启，然后前往设备列表界面选择设备连接！").canceledOnTouchOutside(false).positiveText("确定").negativeText("取消").titleColorRes(R.color.gray_text4).contentColorRes(R.color.gray_text4).onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                startActivity(new Intent(BaseActivity.this, BedLampListActivity.class));
            }
        }).show();
    }

    public void showSucDialog() {
        new MaterialDialog.Builder(this).backgroundColorRes(R.color.white).content("连接成功！").canceledOnTouchOutside(false).positiveText("确定").contentColorRes(R.color.gray_text4).show();
    }

    public void showErrorDialog() {
        new MaterialDialog.Builder(this).backgroundColorRes(R.color.white).content("连接失败！").canceledOnTouchOutside(false).positiveText("确定").contentColorRes(R.color.gray_text4).show();
    }

    protected void initWidget() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("activity_state", this.getClass().getName() + "---------onCreate ");
        super.setContentView(R.layout.activity_base);
        initToolbar();
    }

    @Override
    public void setContentView(int layoutId) {
        setContentView(View.inflate(this, layoutId, null));
    }

    @Override
    public void setContentView(View view) {
        rootLayout = (LinearLayout) findViewById(R.id.root_layout);
        if (rootLayout == null) return;
        rootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        AnnotateUtil.initBindView(this);
        initData();
        initWidget();
    }

    protected void setRootBackgroundRes(int res) {
        rootLayout.setBackgroundResource(res);
    }

    private void initToolbar() {
        toolBar = (Toolbar) findViewById(R.id.toolbar);
        toolbarTitle = (TextView) toolBar.findViewById(R.id.toolbar_title);
        toolBar.setTitle("");
        rightTv = (TextView) findViewById(R.id.toolbar_right);
        setSupportActionBar(toolBar);
    }

    public void hideToolbar() {
        if (toolBar != null && toolBar.getVisibility() == View.VISIBLE) {
            toolBar.setVisibility(View.GONE);
        }
    }

    public void showRightBtn(boolean isShow) {
        if (isShow) {
            rightTv.setVisibility(View.VISIBLE);
            rightTv.setText(null);
            rightTv.setBackgroundResource(R.drawable.ic_setting);
        } else {
            rightTv.setVisibility(View.GONE);
        }
    }

    public void showBackBtn(boolean isShow) {
        if (isShow) {
            toolBar.setNavigationIcon(R.drawable.icon_back);
            toolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            toolBar.setNavigationIcon(null);
            toolBar.setNavigationOnClickListener(null);
        }
    }

    public TextView getRightTv() {
        return rightTv;
    }

    public void setRightText(String text) {
        if (rightTv.getVisibility() == View.GONE) {
            rightTv.setVisibility(View.VISIBLE);
        }
        rightTv.setBackground(null);
        rightTv.setText(text);
    }

    public void setRightText(int textRes) {
        if (rightTv.getVisibility() == View.GONE) {
            rightTv.setVisibility(View.VISIBLE);
        }
        rightTv.setBackground(null);
        rightTv.setText(textRes);
    }


    @Override
    protected void onPause() {
        Log.d("activity_state", this.getClass().getName() + "---------onPause ");
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(this.getClass().getName());
    }

    @Override
    protected void onStart() {
        Log.d("activity_state", this.getClass().getName() + "---------onStart ");
        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.d("activity_state", this.getClass().getName() + "---------onStop ");
        super.onStop();
    }

    @Override
    protected void onRestart() {
        Log.d("activity_state", this.getClass().getName() + "---------onRestart ");
        super.onRestart();
    }

    @Override
    public void onResume() {
        Log.d("activity_state", this.getClass().getName() + "---------onResume ");
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(this.getClass().getName());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("activity_state", this.getClass().getName() + "---------onDestroy ");
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (toolbarTitle != null) {
            toolbarTitle.setText(title);
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            moveTaskToBack(false);
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void onClick(View v) {

    }

}
